<p>The instance passed to the <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/lock"><code>lock</code>
statement</a> should be a dedicated private field.</p>
<h2>Why is this an issue?</h2>
<p>If the instance representing an exclusively acquired lock is publicly accessible, another thread in another part of the program could accidentally
attempt to acquire the same lock. This increases the likelihood of <a href="https://en.wikipedia.org/wiki/Deadlock">deadlocks</a>.</p>
<p>For example, a <code>string</code> should never be used for locking. When a <code>string</code> is <a
href="https://en.wikipedia.org/wiki/Interning_(computer_science)">interned</a> by the runtime, it can be shared by multiple threads, breaking the
locking mechanism.</p>
<p>Instead, a dedicated private <a href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.lock?view=net-9.0"><code>Lock</code></a> object
instance (or <code>object</code> instance, for frameworks before .Net 9) should be used for locking. This minimizes access to the lock instance and
therefore prevents accidential lock sharing.</p>
<p>The following objects are considered potentially prone to accidental lock sharing:</p>
<ul>
  <li> a reference to <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/this">this</a>: if the instance is publicly
  accessibly, the lock might be shared </li>
  <li> a <a href="https://learn.microsoft.com/en-us/dotnet/api/system.type">Type</a> object: if the type class is publicly accessibly, the lock might
  be shared </li>
  <li> a <a href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/">string</a> literal or instance: if any other part of the
  program uses the same string, the lock is shared because of interning </li>
</ul>
<h2>How to fix it</h2>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
void MyLockingMethod()
{
    lock (this) // Noncompliant
    {
        // ...
    }
}
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
#if NET9_0_OR_GREATER
private readonly Lock lockObj = new();
#else
private readonly object lockObj = new();
#endif

void MyLockingMethod()
{
    lock (lockObj)
    {
        // ...
    }
}
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
  <li> Wikipedia - <a href="https://en.wikipedia.org/wiki/Thread_(computing)">Thread</a> </li>
  <li> Wikipedia - <a href="https://en.wikipedia.org/wiki/Lock_(computer_science)">Locking</a> </li>
  <li> Wikipedia - <a href="https://en.wikipedia.org/wiki/Deadlock">Deadlock</a> </li>
  <li> Wikipedia - <a href="https://en.wikipedia.org/wiki/Interning_(computer_science)">Interning</a> </li>
  <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.string.intern#remarks">String interning by the runtime</a> </li>
  <li> Microsoft Learn - <a href="https://docs.microsoft.com/en-us/dotnet/standard/threading/managed-threading-best-practices">Managed Threading Best
  Practices</a> </li>
  <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/lock">The lock statement - ensure
  exclusive access to a shared resource</a> </li>
</ul>

